var Overlay = {
  init: function() {
    var contextMenu = document.getElementById("contentAreaContextMenu");
    if(contextMenu) {
      contextMenu.addEventListener("popupshowing", showHideInsertIP, false);
    }

    window.removeEventListener("load", function() { Overlay.init(); }, true);
  }
};

window.addEventListener("load", function() { Overlay.init(); }, true);


function showHideInsertIP() {
  var insertIPElement = document.getElementById('insertip-context-menu');
  insertIPElement.hidden = !gContextMenu.onTextInput;
}


function insertIP() {
  var elm = document.commandDispatcher.focusedElement;
  var myInsertIP = Components.utils.import("resource://insertip/insertip.jsm", null);
  var ip = myInsertIP.getIP();

  if(elm.selectionStart || elm.selectionStart == '0') {
    var curPos = elm.selectionEnd;
    elm.value = elm.value.substring(0, curPos) + ip + elm.value.substring(curPos, elm.value.length);
    elm.selectionStart = curPos + ip.length;
    elm.selectionEnd = curPos + ip.length;
  }
}
